<?php
namespace Admin\Controller;
use Think\Controller;

class CouponController extends AdminController
{
    //优惠券首页
    public function index(){
        $cate = A("Coupon","Event");
        $res = $cate -> index();
//        $this->assign('today',$today);
        $this->assign('res',$res);
        $this->display('Coupon/index');
    }
    //添加首页
    public function add(){

        $this->display('Coupon/add');
    }
//    添加执行
    public function adddo(){
//        dump($_POST);
        $ms = M('coupon'); // 实例化User对象
        $data['name'] = $_POST['name'];
        $name = $_POST['name'];
        $data['cou_pri'] = $_POST['cou_pri'];
        $data['num'] = $_POST['num'];
        $data['min_pri'] = $_POST['min_pri'];
        $data['is_open'] = $_POST['is_open'];
        $data['begin_time'] = $_POST['begin_time'];//开始时间
        $data['end_time'] = $_POST['end_time'];//结束时间
        //判断开始时间/结束时间是否设置错误
        $a = strtotime($_POST['begin_time']);
        $b = strtotime($_POST['end_time']);
        if($b<$a){
            $this->error('结束时间设置错误,请重新设置',U('Coupon/index'));
        }
        //判断优惠券是否存在
        $res = $ms->field(array('id'))->where('name='."'$name'")->select();
            $res = $res[0];
//        print_r($res);
            if($res){
                $this->error('优惠券名已存在',U('Coupon/add'));
            }else{
            //不存在则可以插入
            //插入数据库
            $res = $ms->add($data);
            if ($res>0){
                $this->success('添加优惠券成功',U('Coupon/index'));
            }else{
                $this->success('添加优惠券失败',U('Coupon/add'));
            }
        }
    }
    public function edit(){
        $id = I('get.id',0,'int');
        $ms = M('coupon');
        $res = $ms->where("id='$id'")->find();

        $open1 = $res['is_open']==0?'checked':'';
        $open2 = $res['is_open']==1?'checked':'';
        $this->assign('open1',$open1);
        $this->assign('open2',$open2);
        $this->assign('res',$res);
        $this->display('Coupon/edit');
    }
    public function editdo(){
        $ms = M("coupon"); // 实例化User对象
        $data['name'] = $_POST['name'];
        $id = $_POST['id'];
        $name = $_POST['name'];
        $data['cou_pri'] = $_POST['cou_pri'];
        $data['num'] = $_POST['num'];
        $data['min_pri'] = $_POST['min_pri'];
        $data['is_open'] = $_POST['is_open'];
        $data['begin_time'] = $_POST['begin_time'];
        $data['end_time'] = $_POST['end_time'];
        //判断开始时间/结束时间是否设置错误
        $a = strtotime($_POST['begin_time']);
        $b = strtotime($_POST['end_time']);
        if($b<$a){
            $this->error('结束时间设置错误,请重新设置',U('Coupon/index'));
        }

        //判断优惠券是否存在
//        $res = $ms->field(array('id'))->where('name='."'$name'")->select();
//        $res = $res[0];
//
//        if($res){
//            $this->error('优惠券名已存在',U('Coupon/index'));
//        }else{
            $res = $ms->where("id='$id'")->save($data);
//          dump($res);die;
            if($res){
                $this->success('修改成功',U('Coupon/index'));
            }else{
                $this->error('修改失败',U('Coupon/index'));
            }
//        }

    }
    public function del(){
        if(IS_GET){
            $id = I('get.id',0,'int');
            $a = M('coupon');
            $res = $a->field(array('receive_num'))->where("id='$id'")->find();
            if ($res && $res['receive_num']>0){
                $this->error('优惠券已被领取,不能删除',U('Coupon/index'));
            }else{
                if(M('coupon')->delete($id)>0){
                    $this->success('删除优惠券成功',U('Coupon/index'));
                }else{
                    $this->error('删除优惠券失败',U('Coupon/index'));
                }
            }
        }else{
            E('非法访问');
        }
    }
    public function give(){
        $ms = M('coupon');
        $res = $ms->field(array('name','id'))->where('is_lose=1 AND is_open=1')->select();
//        dump($res);die;
        $this->assign('list',$res);
        $this->display('Coupon/give');
    }
    public function givedo(){
        //通过name查是否有次用户
        $name = I('post.name');//传来的用户名
        $num = I('post.num',0,'int');//传来的数量
        $id = I('post.id');//传来的优惠券id
        $user = M('user');
        $resid = $user->field(array('id'))->where('name='."'$name'")->find();

        //判断用户是否存在
        if(!$resid){
            $this->error('用户名不存在,请查证后发放',U('Coupon/give'));
        }else{
            $ms = M('coupon');
            $res = $ms->field(array('num'))->where('id='."'$id'")->find();
            //判断数量是否够
            if($res){
                if($num >$res['num']){
                   $this->error("库存不足,当前优惠券最大为".$res['num'],U('Coupon/give'));
                }else{
                    //执行优惠券的发放,优惠券数量的减少
                    $user_coupon = M('user_coupon'); // 实例化User对象
                    $data['user_id'] = $resid['id'];
                    $data['coupon_id'] = $id;//优惠券id
                    $data['num'] = $num;//传来的数量
                    $Model = new \Think\Model();
                    //判断是否已经有次优惠券,如果没有则新加,,,如果有,则更新
                    $user_id = $resid['id'];
                    $res_exi = $user_coupon->where('user_id="'.$user_id.'" AND coupon_id="'.$id.'" ')->find();

                    //更新优惠券shopping_coupon
                    $res2 = $Model->execute("UPDATE shopping_coupon SET receive_num=receive_num+$num,num=num-$num WHERE id='$id'");

                    if($res_exi){
                        //存在则更新
                        $res1 = $user_coupon->where("user_id=$user_id")->setInc('num',$num);
                    }else{
                        //不存在则新建
                        $res1 = $user_coupon->add($data);
                    }
                    //更新优惠券的数量shopping_coupon
                    if ($res1>0 && $res2>0){
                        $this->success('发放成功',U('Coupon/index'));
                    }else{
                        $this->success('发放失败',U('Coupon/index'));
                    }


                }
            }
        }
    }
    public function user(){
        //获取用户id
        $id = I('get.id',0,'int');
        $ms = M('user_coupon');
        $Model = new \Think\Model();
        $list = $Model->table('shopping_coupon AS c,shopping_user_coupon AS uc')->where("c.id = uc.coupon_id AND user_id=$id")->select();
        $today = mktime();//即时时间戳
        foreach($list as $k =>$val){
            $end_time = strtotime($list[$k]['end_time']);
            $id = $list[$k]['id'];
            if($end_time<=$today){
//                $ms = M("coupon"); // 实例化User对象
                $data['is_lose'] = 0;
                $res1 = $ms->where("id='$id'")->save($data);
            }else{
                $data['is_lose'] = 1;
                $res1 = $ms->where("id='$id'")->save($data);
            }
        }

//        $res = $ms->where("user_id='$id'")->select();
        $this->assign('res',$list);
        $this->display('Coupon/user');
    }
    public function is_open(){
        $ms = M("coupon"); // 实例化User对象
        $b = I('get.sub')==0?'1':'0';
//        dump($b);die;
        $data['is_open'] = $b;
        $id = I('get.id',0,'int');//优惠券id
//        dump($id);die;
        $res = $ms->where("id='$id'")->save($data);
//        dump($res);die;
        header("Location:".$_SERVER['HTTP_REFERER']);//改变后返回

    }



}